package com.liusuai.yixian.login;

import com.alibaba.fastjson.JSONObject;
import com.liusuai.yixian.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class RegestService {

    @Autowired //自动连接到UserService Bean
    private UserMapper userMapper;

    public String regest(JSONObject jsonParam) {
        String name = jsonParam.getString("name");
        String pwd = jsonParam.getString("pwd");
        String phone = jsonParam.getString("phone");
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(pwd) || StringUtils.isEmpty(phone)) {
            return "failed empty";
        }


        UUID uuid = UUID.randomUUID();
        String userId = UUID.randomUUID().toString();//用来生成数据库的主键id非常不错..

        UserBean userBean = new UserBean();
        userBean.setName(name);
        userBean.setPwd(pwd);
        userBean.setPhone(phone);
        userBean.setId(userId);

        List<UserBean> userBeans = userMapper.selectUser(name);

        if (userBeans != null && userBeans.size() > 0) {
            System.out.println("已经存在该用户");
            return "failed";
        } else {
            int num = userMapper.insert(userBean);

            if (num > 0) {
                return "success";
            }
        }


        return "failed";
    }
}
